package com.scorpio.common.utils.http;


import com.scorpio.common.constant.Constants;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dell on 2018/8/9.
 */
public class Request {
    private String encoding = Constants.UTF8;
    private String url = "";
    private String method = "GET";
    private String contentType = "application/json;charset=" + encoding;
    private Map<String, String> params;
    private Map<String, String> header;
    private String body;
    private int connectTime = 5000;

    public Request() {
    }

    public Request url(String url) {
        this.url = url;
        return this;
    }

    public Request setConnectTimeout(int connectTime) {
        this.connectTime = connectTime;
        return this;
    }

    public Request get() {
        this.method = "GET";
        return this;
    }


    public Request post() {
        this.method = "POST";
        return this;
    }

    //设置请求头协议
    public Request setContentType(String contentType) {
        this.contentType = contentType;
        return this;
    }

    //初始化text协议
    public Request initTextJson() {
        this.contentType = "text/json;charset=" + encoding;
        return this;
    }

    //初始化json协议
    public Request initApplicationJson() {
        this.contentType = "application/json;charset=" + encoding;
        return this;
    }

    //初始化html协议
    public Request initTextHtml() {
        this.contentType = "text/html;charset=" + encoding;
        return this;
    }

    //初始化form协议
    public Request initformUrlencoded() {
        this.contentType = "application/x-www-form-urlencoded;charset=" + encoding;
        return this;
    }

    //请求参数
    public Request params(Map<String, String> params) {
        this.params = params;
        return this;
    }

    public void encoding(String encoding) {
        this.encoding = encoding;
    }

    public String getUrl() {
        String path = "";
        if (params != null && params.size() > 0) {
            path += "?" + getRequeseParameter();
        }
        return url + path;
    }


    public String getBody() {
        if (body == null) {
            return "";
        }
        return body;
    }

    public Map<String, String> getHeader() {
        if (header == null) {
            return new HashMap<>();
        }
        return header;
    }

    public Request header(Map<String, String> header) {
        this.header = header;
        return this;
    }

    public Request body(String body) {
        this.body = body;
        return this;
    }

    public String getRequeseParameter() {
        String d = "";
        if (params != null) {
            for (String key : params.keySet()) {
                if (d.length() > 1) d += "&";
                try {
                    d += key + "=" + URLEncoder.encode(params.get(key), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        return d;
    }

    public String getMethod() {
        return method;
    }

    public String getContentType() {
        return contentType;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public int getConnectTime() {
        return connectTime;
    }

    public String getEncoding() {
        return encoding;
    }

}
